﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    /// <summary>
    /// 信赖性试验：出货报告
    /// </summary>
    public class R025: BaseReport
    {
        public override void GenReportSheet(IWorkbook workbook, ISheet sheet, OrtHeadModel head)
        {
            #region 表头
            int headColumnsIndex = 0;
            int headCount = (int)(Math.Ceiling(head.Dtls.Count / 6d));
            for (int i = 0; i < 1; i++)
            {
                try
                {
                    //1.Customer:
                    if(sheet.GetRow(4).GetCell(headColumnsIndex + 1) != null)
                    sheet.GetRow(4).GetCell(headColumnsIndex+1).SetCellValue(head.CustomerCode);

                    sheet.GetRow(4).GetCell(headColumnsIndex+5).SetCellValue(head.LotNo);

                    sheet.GetRow(7).GetCell(headColumnsIndex + 1).SetCellValue(head.Pn);
                    sheet.GetRow(7).GetCell(headColumnsIndex + 5).SetCellValue(head.Series);

                    sheet.GetRow(10).GetCell(headColumnsIndex + 1).SetCellValue(head.CustomerPn);
                    sheet.GetRow(10).GetCell(headColumnsIndex + 5).SetCellValue(head.DateCode);

                    sheet.GetRow(13).GetCell(headColumnsIndex + 1).SetCellValue(head.TestDate.ToString("yyyy/M/d"));
                    sheet.GetRow(13).GetCell(headColumnsIndex + 5).SetCellValue(head.DeliveryQty);

                    sheet.GetRow(16).GetCell(headColumnsIndex + 1).SetCellValue(head.ElectricQty);


                    //Tested by:王燕
                    sheet.GetRow(35).GetCell(headColumnsIndex + 6).SetCellValue(head.CreateUser);
                    sheet.GetRow(35).GetCell(headColumnsIndex + 4).SetCellValue(head.ApproveUser);

                    
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }
            }
            #endregion 表头

            #region 细表
            int currentDataColumnIndex = 1;
            int dataRowIndex = 20;
            int currentDtlColumnIndex = 1;
            int currentHeadRowIndex = 19;
            int dtlColumns = 4;
            int dtlRowsCount = 9;

            string RT = "RT:   ";

            for (int i = 0; i < head.Dtls.Count; i++)
            {
                if (i > 1) break;

                try
                {

                    if (i == 0)
                    {
                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex).SetCellValue(
                            RT + head.Dtls[i].GetTemp().Replace("℃", string.Empty)
                            );

                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex + 2).SetCellValue(
                            head.Dtls[i].GetHumidity().Replace("%", string.Empty)
                            );

                        /*
                             CAP(120Hz)  
   + 20    - 20  %
          ~         uF
                         */
                        sheet.GetRow(currentHeadRowIndex).GetCell(headColumnsIndex + 1)
                            .SetCellValue("CAP(120Hz)" + Environment.NewLine + 
                            "+" + head.CapMax + "%  " + head.CapMin + "%" + Environment.NewLine
                            + head.Standard.CapMax + "~" + head.Standard.CapMin + "uF");

                        sheet.GetRow(currentHeadRowIndex).GetCell(headColumnsIndex + 2)
                            .SetCellValue("tanδ " + head.DfMax / 100d + "%" + Environment.NewLine + "(120Hz)");

                        sheet.GetRow(currentHeadRowIndex).GetCell(headColumnsIndex + 3)
                            .SetCellValue("LC: " + head.LcMax + "uA" + Environment.NewLine + "(" +head.LcTime +  " min)");

                        sheet.GetRow(currentHeadRowIndex).GetCell(headColumnsIndex + 4)
                            .SetCellValue("ESR at: " + Environment.NewLine 
                            + "120Hz  25℃" + Environment.NewLine 
                            + Math.Round(head.EsrMax, 3) +  " Ω");

                        sheet.GetRow(currentHeadRowIndex).GetCell(headColumnsIndex + 5)
                            .SetCellValue("Impedance" + Environment.NewLine
                            + "(100kHz 20℃)" + Environment.NewLine
                            + Math.Round(head.ZMax/1000,3) + " Ω");

                        sheet.GetRow(currentHeadRowIndex).GetCell(headColumnsIndex + 6)
                            .SetCellValue("DΦ≦" +  Environment.NewLine
                            + head.Standard.DMax + " (mm)");

                        sheet.GetRow(currentHeadRowIndex).GetCell(headColumnsIndex + 4)
                            .SetCellValue(" L≦" + Environment.NewLine
                            + head.Standard.LMax + " (mm)");

                        sheet.GetRow(currentHeadRowIndex).GetCell(headColumnsIndex + 4)
                            .SetCellValue(" F≦" + Environment.NewLine
                            + head.Standard.CMax + " (mm)");
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }

                #region data
                int cIndex = currentDataColumnIndex;
                var caps = head.Dtls[i].Datas.Where(d => d.ItemCode == "CAP");
                if (caps.Any())
                {
                    var carpss = caps.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat("0.00");
                    //int capNum = OrtReportFactory.GetCapFormatNum(maxlc);
                    for (int k = 0; k < carpss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if(carpss[k].Value!= 0) 
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(carpss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                cIndex++;
                var dfs = head.Dtls[i].Datas.Where(d => d.ItemCode == "DF");
                if (dfs.Any())
                {
                    var dfss = dfs.ToList();
                    for (int k = 0; k < dfss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (dfss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(Math.Round(dfss[k].Value/100,4));
                    }
                }

                cIndex++;
                var lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "LC");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }


                cIndex++;
                lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "ESR");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                cIndex++;
                lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "Z");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                cIndex++;
                lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "D");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        //sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    //sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                cIndex++;
                lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "L");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        //sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    //sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                cIndex++;
                lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "C");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                #endregion data

                currentDtlColumnIndex += dtlColumns;
                currentDataColumnIndex += dtlColumns;
            }

            #endregion

            sheet.ForceFormulaRecalculation = true;
        }
    }
}
